{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1312f275",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "from torch.distributions import Gamma\n",
    "import matplotlib\n",
    "matplotlib.use('Agg')\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "59de58f2",
   "metadata": {},
   "outputs": [],
   "source": [
    "alpha = torch.tensor(1.0)\n",
    "beta = torch.tensor(2.0)\n",
    "\n",
    "# Pytorch distributions model Gamma distributions using alpha and 1/beta (called scale)\n",
    "gamma_dist = Gamma(alpha, rate=beta)\n",
    "assert gamma_dist.mean == alpha/beta"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a0a8f165",
   "metadata": {},
   "source": [
    "### Interactive Visualization\n",
    "Here we allow the user to set different values for the alpha and beta of a gamma distribution and visualise the resulting distribution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "dad96115",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "626afa35565440cdbfe53bbcc9fc22bd",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d06211d169eb4d19ae3367491fab10ad",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "interactive(children=(IntSlider(value=3, description='alpha', max=5, min=1), IntSlider(value=3, description='b…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%matplotlib widget\n",
    "import ipywidgets as widgets\n",
    "from ipywidgets import interact, interact_manual\n",
    "\n",
    "fig, ax = plt.subplots()\n",
    "\n",
    "ax.set_title(\"Gamma Distribution\")\n",
    "ax.set_ylabel(\"PDF(X)\")\n",
    "ax.set_xlabel(\"X\")\n",
    "\n",
    "\n",
    "@interact\n",
    "def plot_gamma_dist(alpha=(1, 5, 1), beta=(1, 5, 1)):\n",
    "    x = torch.linspace(0.01, 10, 1000)\n",
    "    [l.remove() for l in ax.lines]\n",
    "    gamma_dist = Gamma(alpha, rate=beta)\n",
    "    pdf = gamma_dist.log_prob(x).exp()\n",
    "    ax.set_xlim(0, 10)\n",
    "    ax.set_ylim(0, 1)\n",
    "    ax.plot(x, pdf, 'green')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
